/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

import com.aphrodite.ui.graphics.GraphicEffects;

/**
 * A color picker widget with a wider range of colors
 *<p>
 * Provides all the colors available in a rainbow's spectrum.<br/> The following
 * is a <code>RainbowPicker</code> with width and height of 100px.<br/> <img
 * src="../../../resources/rainpicker.png">
 * </p>
 * 
 * @author kentelt
 * 
 */
// must implement traverse
public final class RainbowPicker extends ColorPicker {

	/**
	 * Create a Color picker widget
	 * <p>
	 * The width and height is 50px.
	 * <p>
	 */
	public RainbowPicker(Theme theme) {
		this(theme, 50, 50);
	}

	/**
	 * Create a Color Picker Widget
	 * 
	 * @param width
	 *            of the picker
	 * @param height
	 *            of the picker
	 */
	public RainbowPicker(Theme theme, int width, int height) {
		super(theme, width, height, true);
		updateModel();
	}

	public void cleanUp() {
		colorArray = null;
	}

	/**
	 * Get the color array currently in use.
	 */
	public int[] getColorArray() {
		return colorArray;
	}

	public int getSelectedColor() {
		return colorArray[currentCol * height + currentRow];
	}

	public boolean moveDown() {
		if (currentRow < height - 1) {
			currentRow++;
			reDrawWidget();
			return true;
		} else {
			isInFocus = false;
			return false;
		}
	}

	public boolean moveLeft() {
		if (currentCol > 0) {
			currentCol--;
			reDrawWidget();
			return true;
		}
		return false;
	}

	public boolean moveRight() {
		if (currentCol < width - 1) {
			currentCol++;
			reDrawWidget();
			return true;
		}
		return false;
	}

	public boolean moveUp() {
		if (currentRow > 0) {
			currentRow--;
			reDrawWidget();
			return true;
		} else {
			isInFocus = false;
			return false;
		}
	}

	public boolean pointerDrag(int px, int py) {
		return pointerPress(px, py);
	}

	public boolean pointerPress(final int px, int py) {
		if (super.pointerPress(px, py)) {
			py -= titleHeight;
			currentCol = py - y;
			currentRow = px - x;
			System.out.println("current row" + currentRow + " current col " + currentCol);
			reDrawWidget();
			super.fire();
			return true;
			// }
		}
		return false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		g.drawRGB(colorArray, 0, width, 0, 0, width, height, false);
		g.setColor(getSelectedColor());
		g.fillRect(currentRow - blockWidth / 2, currentCol - blockHeight / 2, blockWidth, blockHeight);
		g.setColor(0);
		g.drawRect(currentRow - blockWidth / 2, currentCol - blockHeight / 2, blockWidth, blockHeight);
		g.setColor(0xfffffff);
		g.drawRect(currentRow - (blockWidth / 2) + 1, currentCol - (blockHeight / 2) + 1, blockHeight - 2,
				blockHeight - 2);
		release(g, width, height, true);

	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) {
		return false;
	}

	protected void updateModel() {
		colorArray = GraphicEffects.getRainbowGradient(width, height);
		currentCol = 0;
		currentRow = 0;
		/* Here block width is the width of the selection rectangle */
		blockWidth = (10 * width) / 100;
		blockHeight = (10 * height) / 100;
	}

}
